package com.edee.bigday.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edee.bigday.commodity.dao.AttrGroupDao;
import com.edee.bigday.commodity.entity.AttrEntity;
import com.edee.bigday.commodity.entity.AttrGroupEntity;
import com.edee.bigday.commodity.service.AttrGroupService;
import com.edee.bigday.commodity.service.AttrService;
import com.edee.bigday.commodity.vo.AttrGroupWithAttrsVo;
import com.edee.bigday.commodity.vo.SpuItemAttrGroupVo;
import com.edee.common.utils.PageUtils;
import com.edee.common.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catalogId) {
        String key = (String) params.get("key");
        //select * from cms_attr_group where catalog_id=? and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catalogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catalog_id", catalogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类id查出所有分组及分组中的属性
     *
     * @param catalogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatId(Long catalogId) {
        //1、查询分组信息：
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catalog_id", catalogId));
        //2、查询分组中所有属性：
        if (attrGroupEntities == null || attrGroupEntities.size() == 0) {
            return null;
        }
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            List<AttrEntity> attrs = attrService.getRaletionAttr(attrsVo.getAttrGroupId());
            if (attrs != null) {
                attrsVo.setAttrs(attrs);
            }
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spuId对应的所有属性的分组信息，以及当前分组下所有属性与属性值：
        //针对cms_product_attr_value表中，进行如下分析：
        //1）查出当前spu对应的所有属性分组(groupName:{attrName:attrValue})
        //  在cms_attr_group表中，属性分组attr_group_id关联了catalog_id，所有，只要知道当前spu所属的分类，就可以知道其所在分组。
        //  在cms_spu_info表中，可以查出当前spuId对应的catalog_id，可以在com/edee/bigday/commodity/service/impl/SkuInfoServiceImpl.java中，
        //  利用已经获取到的SkuInfoEntity查询到对应的catalog_id。
        //2）先SELECT ag.attr_group_name,ag.attr_group_id FROM `cms_attr_group` ag WHERE catalog_id = 225，
        // 再从cms_attr_attrgroup_relation表中根据attr_group_id查到对应的属性id（attr_id）。
        // 最后，再从cms_attr和cms_product_attr_value分别查到attr_name和attr_value：
//        AttrGroupDao baseMapper = this.getBaseMapper();
        AttrGroupDao baseMapper = this.baseMapper;
        //上面的this.getBaseMapper()和this.baseMapper均可。
        List<SpuItemAttrGroupVo> spuItemAttrGroupVos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return spuItemAttrGroupVos;
    }
}